﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

public partial class userControls_ctrl_myTrails : System.Web.UI.UserControl
{
    private IList<Bintje.Trail> m_trails;

    public int? UserId
    {
        get
        {
            return (hdnMyTrail.Value!="")?int.Parse(hdnMyTrail.Value):0;
        }
        set
        {
            hdnMyTrail.Value = value.ToString();
        }
    }
    public bool IsEdit
    {
        get
        {
            return (hdnIsEdit.Value == "1") ? true :false ;
        }
        set {
            hdnIsEdit.Value = (value) ? "1" : "0";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (UserId != null)
            {
                int uid = UserId.Value;
                
                if (uid > 0)
                {
                    hrpmyrails.NavigateUrl = string.Format("~/UI/view-MyTrails.aspx?UserId={0}", uid.ToString());
                    m_trails = Bintje.Trail.Load(Session["Session"] as Bintje.bintje.imageoSession);
                    var myTrails = (from trl in m_trails
                                    orderby trl.Id
                                    select new { ID = trl.Id, Name = trl.Title, 
                                        LocationLatitude = trl.Latitude, 
                                        LocationLongitude = trl.Longitude.ToString(),
                                        LocationCountry = trl.Waypoints.Count > 0 ? trl.Waypoints[0].Country : string.Empty,
                                        LocationCity = trl.Waypoints.Count > 0 ? trl.Waypoints[0].City : string.Empty, 
                                        Category = string.Empty, Length = string.Empty }).ToArray();
                    if (myTrails.Length > 0)
                    {
                        lstMyTrails.DataSource = myTrails.Take(2);
                        lstMyTrails.DataBind();
                        lbltrails.Visible = false;
                    }
                    else
                    {
                        lbltrails.Text = "No Trails found";
                        hrpmyrails.Visible = false;
                    }
                    string usrName = AppUser.getUsernameById(uid);
                    lblUsername.Text = usrName.ToUpper() + "'s Trails";
                   // hrpmyrails.Text = string.Format("VIEW {0}'s TRAILS", usrName.ToUpper());
                    hrpmyrails.Text = "VIEW MORE";
                    if (IsEdit)
                    {
 
                    }
                }
            }
        }
    }

    protected void mytrails_databound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Image imgTrail = (Image)e.Item.FindControl("imgTrail");
            Literal ltrid = (Literal)e.Item.FindControl("ltrid");
            HyperLink hprEdit = (HyperLink)e.Item.FindControl("hprEdit");
            HyperLink hprGrantPermission = (HyperLink)e.Item.FindControl("hprGrantPermission");

            if (!IsEdit)
            {
                hprEdit.Visible = false;
                hprGrantPermission.Visible = false;
            }
            /*
            dbTrailInfoDataContext dbo = BL.dbTrail;
            var img = (from i in dbo.Markers
                       where i.TrailID == int.Parse(ltrid.Text) && i.Picture.Trim() != ""
                       select new { i.Picture }).Take(1).ToArray();
            */
            int trid = 0;
            int.TryParse(ltrid.Text, out trid);
            Bintje.Trail trail = m_trails.Single(t => t.Id == trid);//Bintje.Trail.Get(trid);
            if(null != trail)
            {
                IList<Bintje.WayPoint> waypoints = trail.Waypoints;
                if (null != waypoints && waypoints.Count > 0 && null != waypoints[0].ImageUrl && waypoints[0].ImageUrl.Length > 0 &&
                    !string.IsNullOrEmpty(waypoints[0].ImageUrl[0]))
                {
                    imgTrail.ImageUrl = waypoints[0].ImageUrl[0];
                }
                else
                {
                    imgTrail.ImageUrl = "~/sitefiles/Get-Thumbnail.aspx?f=~/images/no_image.gif&w=50&h=50";
                }
            }
            else
            {
                imgTrail.ImageUrl = "~/sitefiles/Get-Thumbnail.aspx?f=~/images/no_image.gif&w=50&h=50";
            }
            /*
            if (img.Length > 0)
            {
                if (img[0].Picture != null && File.Exists(Server.MapPath("~/upload/marker/" + img[0].Picture.Trim())))
                {
                    imgTrail.ImageUrl = string.Format("~/sitefiles/Get-Thumbnail.aspx?f=~/Upload/Marker/tn/{0}&w=60&h=60", img[0].Picture.Trim());
                }
                else
                {
                    imgTrail.ImageUrl = "~/sitefiles/Get-Thumbnail.aspx?f=~/images/no_image.gif&w=50&h=50";
                }
            }
            else
            {
                imgTrail.ImageUrl = "~/sitefiles/Get-Thumbnail.aspx?f=~/images/no_image.gif&w=50&h=50";
            }
            */
        }
    }
}
